package com.hsd.whb.service.impl;

import com.hsd.whb.entity.Manager;
import com.hsd.whb.entity.Recorder;
import com.hsd.whb.enums.ResultEnum;
import com.hsd.whb.exception.HwbException;
import com.hsd.whb.repository.ManagerRepository;
import com.hsd.whb.repository.RecorderRepository;
import com.hsd.whb.service.RecorderService;
import com.hsd.whb.utils.BeanUtil;
import com.wangjiacai.hqlQuery.service.impl.BaseServiceImpl;
import com.wangjiacai.hqlQuery.util.HqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RecorderServiceImpl extends BaseServiceImpl implements RecorderService {

    @Autowired
    private RecorderRepository recorderRepository;

    @Autowired
    private ManagerRepository managerRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public Recorder findByUsername(String username) {
        return recorderRepository.findByUsername(username);
    }

    @Override
    public Recorder findById(Integer id) {
        return recorderRepository.getOne(id);
    }

    @Override
    public Recorder saveRecorder(Recorder recorder, String username) {
        validSaveRecorder(recorder);
        Manager manager = managerRepository.findByUsername(username);
        recorder.setPassword(passwordEncoder.encode(recorder.getWorkNumber()));
        recorder.setManagerId(manager.getId());
        recorder.setLogincount(0);
        recorder.setDeleted(0);
        return recorderRepository.save(recorder);
    }

    private void validSaveRecorder(Recorder recorder) {
        if (recorderRepository.findByUsername(recorder.getUsername()) != null) {
            throw new HwbException(ResultEnum.USERNAME_EXITS);
        }
        if (recorderRepository.findByWorkNumber(recorder.getWorkNumber()) != null) {
            throw new HwbException(ResultEnum.WORKNUMBER_EXITS);
        }
        if (recorderRepository.findByPhone(recorder.getPhone()) != null) {
            throw new HwbException(ResultEnum.PHONE_EXITS);
        }
        if (recorderRepository.findByEmail(recorder.getEmail()) != null) {
            throw new HwbException(ResultEnum.EMAIL_EXITS);
        }
    }

    @Override
    public Recorder updateRecorder(Recorder recorder) {
        validUpdateRecorder(recorder);
        Recorder target = recorderRepository.getOne(recorder.getId());
        BeanUtil.copyPropertiesIgnoreNull(recorder, target);
        return recorderRepository.save(target);
    }

    private void validUpdateRecorder(Recorder recorder) {
        Recorder recorder1 = recorderRepository.getOne(recorder.getId());
        if (!recorder.getPhone().equals(recorder1.getPhone())) {
            if (recorderRepository.findByPhone(recorder.getPhone()) != null) {
                throw new HwbException(ResultEnum.PHONE_EXITS);
            }
        }
        if (!recorder.getEmail().equals(recorder1.getEmail())) {
            if (recorderRepository.findByEmail(recorder.getEmail()) != null) {
                throw new HwbException(ResultEnum.EMAIL_EXITS);
            }
        }
    }

    @Override
    public void deleteRecorderById(Integer id) {
        Recorder recorder = recorderRepository.getOne(id);
        recorder.setDeleted(1);
        recorderRepository.save(recorder);
    }

    @Override
    public List<Recorder> findRecorderList(Pageable pageable, String username, Recorder recorder) {
        Manager manager = managerRepository.findByUsername(username);
        recorder.setManagerId(manager.getId());
        recorder.setDeleted(0);
        return this.find(HqlUtil.getQueryHql(recorder), recorder, pageable,Recorder.class);
    }

    @Override
    public long findRecorderListTotalPages(String username, Recorder recorder) {
        Manager manager = managerRepository.findByUsername(username);
        recorder.setManagerId(manager.getId());
        recorder.setDeleted(0);
        return this.count(HqlUtil.getCountHql(recorder), recorder);
    }


}
